<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="formData"
        ref="formRef"
        :inline="true"
        label-width="110px"
        v-loading="formLoading"
      >
        <el-form-item label="三级功能模块" prop="classC">
          <el-input
            v-model="formData.classC"
            clearable
            @keyup.enter="handleQuery"
            class="!w-140px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        ref="multipleTableRef"
        border
        row-key="id"
        :height="tableHeight"
        v-loading="loading"
        :data="list"
        @select-all="selectAll"
        @select="select"
        @selection-change="handleSelectionChange"
        selection-type="none"
      >
        <el-table-column align="center" type="selection" />
        <el-table-column label="一级功能模块" prop="classA" />
        <el-table-column label="二级功能模块" prop="classB" />
        <el-table-column label="三级功能模块" prop="classC" />
      </el-table>
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import * as AbnormalApi from '@/api/config/exceptionManagement/abnormal'
  // import { DICT_TYPE } from '@/utils/dict'
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const multipleTableRef = ref()
  const formData = ref({
    classC: ''
  })
  const tableHeight = ref(0)
  const list = ref<any>([]) // 列表的数据
  const passData = ref<any>([])
  const formRef = ref() // 表单 Ref

  /** 打开弹窗 */
  const open = async (list) => {
    tableHeight.value = document.body.clientHeight - 132 - 127 - 200
    passData.value = list
    selectList.value = list
    dialogVisible.value = true
    getCustomerProfileList()
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    let params = {
      list: selectList.value
    }
    // 校验表单
    emit('success', params)
    dialogVisible.value = false
  }
  //重置按钮
  const resetQuery = () => {
    formData.value.partName = undefined
    formData.value.drawingNumber = undefined
    formData.value.pageNo = 1
    formData.value.pageSize = 10
    handleQuery()
  }
  /** 搜索按钮操作 */
  const handleQuery = () => {
    getCustomerProfileList()
  }
  const loading = ref(true) // 列表的加载中
  //获取菜单列表
  const getCustomerProfileList = async () => {
    loading.value = true
    try {
      const data = await AbnormalApi.getFunctionPageMenu(formData.value.classC)
      list.value = data
      nextTick(() => {
        if (passData.value.length) {
          for (let i = 0; i < list.value.length; i++) {
            if (list.value[i].id === passData.value[0].id) {
              multipleTableRef.value.toggleRowSelection(list.value[i], true)
            } else {
              multipleTableRef.value.toggleRowSelection(list.value[i], false)
            }
          }
        }
      })
    } finally {
      loading.value = false
    }
  }

  const selectAll = () => {
    multipleTableRef.value.clearSelection()
  }
  const select = (selection, row) => {
    multipleTableRef.value.clearSelection()
    if (selection.length == 0) {
      isDelete.value = true
      return
    }
    multipleTableRef.value.toggleRowSelection(row, true)
    isDelete.value = false
  }
  /**选中 */
  const selectList = ref<any>([])
  const isDelete = ref(true)
  const handleSelectionChange = async (value) => {
    if (value.length == 1) {
      selectList.value = await value
    }
  }
</script>
